package com.cf.controller;


import com.cf.entity.AccountType;
import com.cf.entity.AccountTypeIdType;
import com.cf.entity.Result;
import com.cf.service.AccountTypeIdTypeService;
import com.cf.service.AccountTypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class AccountTypeController {
    @Autowired
    private AccountTypeService accountTypeService;
    @Autowired
    private AccountTypeIdTypeService accountTypeIdTypeService;

    @RequestMapping("/selectAllAccountType")
    public Object selectAllAccountType(){
        List<AccountType> accountTypes = accountTypeService.selectAllAccountType();
        if (accountTypes.size()!=0){
            return new Result(200,"查询成功",accountTypes);
        }
        return new Result(400,"查询失败",null);
    }
    @RequestMapping("/selectAllAccountTypes")
    public Object selectAllAccountTypes(@RequestParam(defaultValue = "1") int pagenum, int pagesize, String name){
        Map map = new HashMap<>();
        map.put("name",name);
        PageInfo<AccountType> pageInfo = accountTypeService.selectAllAccountTypes(pagenum, pagesize, map);
        if (pageInfo.getList().size()!=0){
            return new Result(200,"查询成功",pageInfo);
        }
        return new Result(400,"没有数据",null);
    }


    @RequestMapping("/updateAccountType")
    public Object updateAccountType(AccountType accountType){
        int num = accountTypeService.updateAccountType(accountType);
        if (num!=0){
            return new Result(200,"修改成功",null);
        }
        return new Result(400,"修改失败",null);
    }


    @RequestMapping("/addAccountType")
    public Object addAccountType(AccountType accountType){
        int num = accountTypeService.addAccountType(accountType);
        if (num!=0){
            return new Result(200,"添加成功",null);
        }
        return new Result(400,"添加失败",null);
    }

    @RequestMapping("/delAccountType")
    public Object delAccountType(long id){
        List<AccountTypeIdType> accountTypeIdTypes =
                accountTypeIdTypeService.selectByaccountid(id);
        if (accountTypeIdTypes.size()!=0){
            return new Result(400,"删除失败，此数据不能删除",null);
        }else{
            int num = accountTypeService.delAccountType(id);
            if (num!=0){
                return new Result(200,"删除成功",null);
            }
            return new Result(400,"删除失败",null);
        }
    }


    @RequestMapping("/selectByIds")
    public Object selectByIds(long id){
        AccountType accountType = accountTypeService.selectByIds(id);
        if (accountType!=null){
            return new Result(200,"查到数据",accountType);
        }
        return new Result(400,"没有此数据数据",null);
    }
}
